/*
 * @(#)SpecMouseEvent.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library itfP free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library itfP distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package fr.ema.arch3d.editor.spec;

import org.jgraph.JGraph;
import org.jgraph.graph.BasicMarqueeHandler;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class SpecMouseEvent extends BasicMarqueeHandler implements MouseListener {

    /** Field description */
    public DrawSpecImpl ds;

    /**
     * Constructs ...
     *
     *
     * @param ds
     */
    public SpecMouseEvent(DrawSpecImpl ds) {
        this.ds = ds;
    }

    public void mouseClicked(MouseEvent e) {

//      if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
//              /*Object[] obj = BasicMarqueeHandler.getGraphForEvent(e)
//              .getSelectionCells();
//              if (obj.length != 0) {
//                      System.out.println("ttttttttttttttttttttttttttttttt");
//              }*/
//      
//              JGraph graph=getGraphForEvent(e);
//              int x = e.getX(), y = e.getY();
//              Object cell = graph.getFirstCellForLocation(x, y);
//              Specification spec=ds.spec;
//              
//              if (cell != null&&cell.getClass().getSimpleName().equals("SpecVertex")) {                         
//                      for (int i = 0; i < spec.cmpRole_list.size(); i++) {
//                              if(((ComponentType)spec.cmpRole_list.get(i)).name.equals(cell.toString()))
//                                      {
////                                    ConfigADLText t=new ConfigADLText();
////                                    ds.sText.setDocument(t.createTextCmp((ComponentType)spec.cmpRole_list.get(i)));
////                                    ds.sText.updateUI();
//                                      }                                               
//                      }
//                      
//              }
//              else if (cell != null&&cell.getClass().getSimpleName().equals("SpecEdge")) {
//                      for (int i = 0; i < spec.connection_list.size(); i++) {
//                              if(((Connection)spec.connection_list.get(i)).name.equals(cell.toString()))
//                                      {
//                                      ConfigADLText t=new ConfigADLText();
//                                      ds.sText.setDocument(t.createTextCnt((Connection)spec.connection_list.get(i)));
//                                      ds.sText.updateUI();
//                                      }                                               
//                      }
//              }
//              else{
//                      ConfigADLText t=new ConfigADLText();
//                      ds.sText.setDocument(t.createTextSpec(spec));
//                      ds.sText.updateUI();
//              }
//      }
    }

    public void mouseEntered(MouseEvent e) {
        if ((e.getClickCount() == 1) && (e.getButton() == MouseEvent.BUTTON1)) {}
    }

    public void mouseExited(MouseEvent e) {

        // TODO Auto-generated method stub
    }

    public void mousePressed(MouseEvent e) {
        if ((e.getClickCount() == 1) && (e.getButton() == MouseEvent.BUTTON3)) {

            // startPoint = e.getPoint();
            JGraph graph = getGraphForEvent(e);

            // CellView[] views = graph.getGraphLayoutCache().getCellViews();
            // Get Cell under Mousepointer
            int    x    = e.getX(),
                   y    = e.getY();
            Object cell = graph.getFirstCellForLocation(x, y);

            // Print Cell Label
            if ((cell != null) && cell.getClass().getSimpleName().equals("SpecVertex")) {
                JPopupMenu menu1 = new JPopupMenu();

                // Display PopupMenu
                JMenuItem item1;

                menu1.add(item1 = new JMenuItem("Replace component type"));
                menu1.add(item1 = new JMenuItem("Delete component type"));
                item1.setHorizontalTextPosition(JMenuItem.RIGHT);

                // item.addActionListener(menuListener);
                menu1.show(graph, e.getX(), e.getY());
            } else if ((cell != null) && cell.getClass().getSimpleName().equals("SpecEdge")) {
                JPopupMenu menu1 = new JPopupMenu();

                // Display PopupMenu
                JMenuItem item1;

                menu1.add(item1 = new JMenuItem("Delete connection"));
                item1.setHorizontalTextPosition(JMenuItem.RIGHT);

                // item.addActionListener(menuListener);
                menu1.show(graph, e.getX(), e.getY());
            } else {
                JPopupMenu menu = new JPopupMenu();

                // Display PopupMenu
                JMenuItem item;

                menu.add(item = new JMenuItem("Insert component type"));
                menu.add(item = new JMenuItem("Insert connection"));
                item.setHorizontalTextPosition(JMenuItem.RIGHT);

                // item.addActionListener(menuListener);
                menu.show(graph, e.getX(), e.getY());
            }
        }
    }

    public void mouseReleased(MouseEvent e) {

        // TODO Auto-generated method stub
    }
}
